from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.decorators import action

from .models import *
from .serializers import *
from rest_framework.response import Response
# Create your views here.

# 学生视图集
class StudentViewSet(viewsets.ModelViewSet):
    # # 查询所有学生
    # def list(self, request):
    #     # 返回查询集
    #     students = Student.objects.filter(is_delete=False)
    #     # 序列化把queryset对象转json
    #     serializer = StudentSerializer(students, many=True)
    #     # 返回查询到的json数据相应前端
    #     return Response(serializer.data)
    #
    # # 添加学生数据
    # def create(self, request):
    #     serializer = StudentSerializer(data=request.data)
    #
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     return Response(serializer.data, status=status.HTTP_201_CREATED)
    #
    # # 查询单条学生数据
    # def retrieve(self, request, pk=None):
    #     try:
    #         student = Student.objects.filter(is_delete=False).get(pk=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=status.HTTP_404_NOT_FOUND)
    #     # 创建序列化器
    #     serializer = StudentSerializer(student)
    #     return Response(serializer.data)
    #
    # # 修改学生数据
    # def update(self, request, pk=None):
    #     try:
    #         student = Student.objects.filter(is_delete=False).get(pk=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=status.HTTP_404_NOT_FOUND)
    #     # 获取用户传入的数据，进行反序列化
    #     serializer = StudentSerializer(instance=student, data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     return Response(serializer.data)
    #
    # # 删除学生数据
    # def destroy(self, request, pk=None):
    #     try:
    #         student = Student.objects.get(pk=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=status.HTTP_404_NOT_FOUND)
    #     # 调用模板删除方法
    #     student.delete()
    #     # 删除完没有数据返回，返回状态码
    #     return Response(status=status.HTTP_204_NO_CONTENT)

    # 我们只需要指定 查询集（模型数据对象）
    queryset = Student.objects.filter(is_delete=False)
    # 以及序列化器 （进行序列化和反序列化，序列化器里面映射的是反馈到前端的数据）
    serializer_class = StudentSerializer # 指定使用的序列化器类


# 班级视图集

class ClassViewSet(viewsets.ModelViewSet):
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassSerializer

    # 获取最新班级信息
    @action(methods=["get"],detail=False)
    def last(self,request):
        last_classes = self.queryset.last()
        serializer = self.get_serializer(last_classes)

        return Response(serializer.data)

    # 获取班级最后一个学生信息
    @action(methods=["get"],detail=True)
    def last_student(self,request,pk):
        try:
            classes = Classes.objects.get(id=pk)
        except Classes.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        classes_student = classes.student_set.last()
        serializer = self.get_serializer(classes_student)
        return Response(serializer.data)